package com.hs.global.action;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.auth.model.HsAccount;
import com.hs.auth.model.HsUserinfo;
import com.hs.auth.service.IUserService;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
@Controller  
public class IndexController  extends BaseAction {
    /**
	 * 
	 */
	private static String ROOT_SQL = "SELECT * FROM HS_POWER A WHERE ISROOT=0 AND A.PARENTID=0 ORDER BY ORDERNUM";
	private static String TREENODE_SQL = "SELECT * FROM HS_POWER A WHERE A.PARENTID=?   ORDER BY ORDERNUM";
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(IndexController.class);
	private IUserService userService;
	 @Autowired  
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	 /**
	  * 菜单控制
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/menu.do", method = RequestMethod.GET)   
     public ModelAndView indexMenu(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 HsAccount acc = this.getCurrentuser(request);
         String userName = "";
         if(null != acc){
	         HsUserinfo userinfo = acc.getUserinfo();
	         if(null != userinfo){
	        	 userName = userinfo.getUsername();
	         }
         }
         modelMap.put("username", userName);
		 return new ModelAndView("menu",modelMap);
		 
	 } 
	 /**
	  * 主要header
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/header.do", method = RequestMethod.GET)   
     public ModelAndView indexHeader(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
 		 return new ModelAndView("header",modelMap);
		 
	 } 
	 @RequestMapping("/menutree.do")   
		public void menutree(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
		 	HsAccount accinfo = this.getCurrentuser(request);
			Map pmap = this.getUserRoleByUserid(request, new Long(accinfo.getId()));
			StringBuilder builder = new StringBuilder();
			builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
			builder.append("<tree id=\"0\">\n");
			
			DBMgr dbutil = this.getDbo(request);
			//查询根节点
			List dataList = dbutil.queryList(ROOT_SQL, null);
			if(null != dataList && dataList.size()>0){
				for(int i=0;i<dataList.size();i++){
					Map root = (Map)dataList.get(i);
					String id = root.get("ID")+"";
					builder.append("<item text=\"").append(root.get("POWERNAME")).append("\" open=\"1\" id=\"").append(id).append("\">");
					builder.append("\n"); 
					List secondList = dbutil.queryList(TREENODE_SQL, new Object[]{id});
					for(int j=0;j<secondList.size();j++){
						Map node = (Map)secondList.get(j);
						String idNode = node.get("ID")+"";
	 					boolean isauth = this.isHasAuth(request,pmap,idNode,new Long(accinfo.getId()));
						if(!isauth)
							continue;
						builder.append("<item text=\"").append(node.get("POWERNAME")).append("\"");
						builder.append(" open=\"1\" id=\"").append(idNode).append("\" ");
						builder.append(">\n");
	 					List threeList = dbutil.queryList(TREENODE_SQL, new Object[]{idNode});
						for(int k=0;k<threeList.size();k++){
							Map node3 = (Map)threeList.get(k);
							String idNode3 = node3.get("ID")+"";
							boolean isauth1 = isHasAuth(request,pmap,idNode3,new Long(accinfo.getId()));
							if(!isauth1)
								continue;
							String url = (String)node3.get("URL");
							builder.append("<item text=\"").append(node3.get("POWERNAME")).append("\"");
							builder.append(" id=\"").append(idNode3).append("\">\n ");
							//builder.append(" url=\"").append(url).append("\" ");
							
							builder.append("<userdata name=\"url\">").append(url).append("</userdata>\n"); 
							builder.append("</item>\n");

						}
						builder.append("</item>\n");
					}
					builder.append("</item>\n");
				}
				 
			}
			builder.append("</tree>"); 
			logger.info("builder：" + builder.toString());
			 
	    	try {
				PrintWriter w = response.getWriter(); 
				 w.println(builder.toString());
				 w.flush();
				 w.close();
			} catch (IOException e) {
				 throw e;
			} 
		}
	 /**
	  * 当前位置页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/currentMenu.do", method = RequestMethod.GET)   
     public ModelAndView currentMenu(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 Map menumap = this.getCurrentMenuStr(request);
		 String menustr = (String)menumap.get("menustr");
		 modelMap.put("menustr", menustr);
 		 return new ModelAndView("currentmenu",modelMap);
		 
	 } 
	 /**
	  * 内容页主页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/mainContent.do", method = RequestMethod.GET)   
     public ModelAndView mainContent(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String mainaction = (String)request.getParameter("mainaction");
 		 return new ModelAndView(mainaction,modelMap);
		 
	 } 
	 /**
	  * 内容页主页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/contentFrame.do", method = RequestMethod.GET)   
     public ModelAndView contentFrame(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String mainaction = (String)request.getParameter("mainaction");
 		 return new ModelAndView("middleframe",modelMap);
		 
	 } 
}
